<script type="text/javascript" charset="utf-8">
	$(document).ready(function() {
		$("#tgl").datepicker({
			dateFormat  : "yy-mm-dd",        
			  changeMonth : true,
			  changeYear  : true					
		});
	});
</script>
<?php
if($_SESSION['emergencystock']!=1){
	echo "Akses tidak diizinkan";
	exit();
}

$act = filter($_GET['act']);
$field = filter($_GET['field']);
$q = filter($_GET['q']);

if(empty($act)){	
?>
<div class="title">
	<h5>Stok Emergency</h5>
	<div class="search global-nav">
		<form method="GET" class="form-search js-search-form" action="index.php?stock_emergency" id="global-nav-search" style="width:350px">
			<input type="hidden" name="pages" value="stock_emergency">
			<select name="field" id="birthday_month" aria-label="Month" class="" >
				<option value="" <?php echo ($field=="")?"selected":"";?>>Cari Berdasarkan:</option>
				<option value="tgl" <?php echo ($field=="tgl")?"selected":"";?>>Tanggal</option>
				<option value="jumlah" <?php echo ($field=="jumlah")?"selected":"";?>>Jumlah</option>
				<option value="status" <?php echo ($field=="status")?"selected":"";?>>Status</option>
			</select>

			<label class="visuallyhidden" for="search-query">Search query</label>
			<input class="search-input" type="text" id="search-query" placeholder="Search" name="q" value="<?php echo $q;?>">
			<span class="search-icon js-search-action">
				<button type="submit" class="icon nav-search" tabindex="-1">
					<span class="visuallyhidden">Search</span>
				</button>
			</span>
			<input disabled="disabled" class="search-input search-hinting-input" type="text" id="search-query-hint" autocomplete="off" spellcheck="false">				              					              
		</form>
	</div>	
</div>
<div class="table">										
	<table>
		<thead>
			<tr>
				<th class="selected"><input type="checkbox" class="checkall" /></th>
				<th class="action">No.</th>
				<th class="left">Tanggal</th>
				<th class="left">Jumlah</th>
				<th class="left">Satuan</th>
				<th>Status</th>
				<th>Status Proses </th>
				<th class="action" colspan="2"><a href="?pages=stock_emergency&act=edit" class="icon-plus-sign" title="Tambah Data Baru"></a></th>									
			</tr>
		</thead>
		<tbody>
		<?php			

		$q2=$q;
		if($field=='status'){
			$q2 = (strtolower($q)=='aktif')?'1':'0';
		}
		
		$addQuery='';


		if(!empty($field))
			$addQuery .= " WHERE $field LIKE '%$q2%' ";

		$batas=10;
		$pg=(int) $_GET['pg'];
		if(empty($pg))
		{
			$posisi=0;
			$pg=1;
		}
		else
		{
			$posisi = ($pg-1) * $batas;
		}
		$sqlKode1 = mysql_query("SELECT * FROM  tb_emergency_stock $addQuery ORDER BY tgl  DESC limit $posisi, $batas");
		
		$no=$posisi+1;
		while($dataKode = mysql_fetch_array($sqlKode1)){								

			$status = ($dataKode['status']==1)? "RTU => RTS":"RTS => RTU";
			$status_proses = ($dataKode['status_proses']==1)? "<div class=icon-ok>&nbsp;</div>":"<div class=icon-remove>&nbsp;</div>";
			//if ($dataKode['status_proses']==1) { $blok ="style='visibility:hidden'";}else{$blok ="style='visibility:visible'";}
			if($dataKode['status_proses']==1){
				$blok_edit = "";
				$blok_del = "";
			}else{
				$blok_edit = "href=?pages=stock_emergency&act=edit&id=".$dataKode['id_emergency_stock'];
				$blok_del = "window.location='?pages=stock_emergency&act=del&id=".$dataKode['id_emergency_stock']."'";
			}
		?>
			<tr>
				<td class="selected"><input type="checkbox" /></td>
				<td class="center"><?php echo $no;?></td>
				<td><?php echo $dataKode['tgl'];?></td>
				<td><?php echo $dataKode['jumlah'];?></td>
				<td><?php echo $dataKode['satuan'];?></td>
				<td><?php echo $status;?></td>
				<td><?php echo $status_proses?></td>
				<td class="action">
				<?php if($dataKode['status_proses']!=1){ ?>
					<a href=?pages=stock_emergency&act=edit&id=<? echo $dataKode['id_emergency_stock'] ?> class="icon-edit" title="Ubah Data"></a>
				<?php } ?>
				</td>
				<td class="action">
				<?php if($dataKode['status_proses']!=1){ ?>

					<a href="#" title="Hapus Data" onClick="if(confirm('Anda akan menghapus data ini?')) window.location='?pages=stock_emergency&act=del&id=<?php echo $dataKode['id_emergency_stock'];?>';" class="icon-trash"></a>
				<?php } ?>
				</td>								
				</tr>
		<?php	
			$no++;				
		}

		$sqlSup2 = mysql_query("SELECT * FROM  tb_emergency_stock $addQuery ORDER BY tgl DESC");
		$jumlahData = mysql_num_rows($sqlSup2);

		$file="?pages=stock_emergency&field=$field&q=$q";
		$jmlhalaman=ceil($jumlahData/$batas);

		?>															
		</tbody>
	</table>
	<!-- pagination -->
	<div class="pagination pagination-left">
		<div class="results">
			<span><?php echo "Showing Results ".($posisi+1)."-".($no-1)." of $jumlahData";?></span>
		</div>
		<ul class="pager">
			<?php
			//link ke halaman sebelumnya (previous)
			if($pg > 1){
				$previous=$pg-1;
				echo "<li><a href=$file&pg=1>&laquo; First</a></li>
				<li><a href=$file&pg=$previous>&laquo; Prev</a></li> ";
			}else{ 
				echo "<li class='disabled'>&laquo; First</li>
				<li class='disabled'>&laquo; Prev</li> ";
			}
			?>

			<?php
			//Link untuk Angka
			$angka=($pg > 3 ? " <li class='separator'>...</li> " : " ");
			for($i=$pg-2;$i<$pg;$i++)
			{
			  if ($i < 1) 
				  continue;
			  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
			}

			$angka .= " <li class='current'>$pg</li>";
			for($i=$pg+1;$i<($pg+3);$i++)
			{
			  if ($i > $jmlhalaman) 
				  break;
			  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
			}

			$angka .= ($pg+2<$jmlhalaman ? " <li class='separator'>...</li> <li><a href='$file&pg=$jmlhalaman'>$jmlhalaman</a></li>" : " ");

			echo "$angka";
			?>
			

			<?php
			//link kehalaman berikutnya (Next)
			if($pg < $jmlhalaman)
			{
				$next=$pg+1;
				echo "<li><a href='$file&pg=$next'>Next &raquo;</a></li>
				<li><a href='$file&pg=$jmlhalaman'>Last &raquo;</a></li>";
			}
			else
			{ 
				echo "<li class='disabled'>Next &raquo;</li>
				<li class='disabled'>Last &raquo;</li> ";
			}		
			?>													
		</ul>
	</div>

	<!-- end pagination -->
	<!-- table action -->
	
	<!-- end table action -->						
</div>
<?php
}elseif ($act=='edit') {

	$id_stok = (int) filter($_GET['id']);

	$sqlSup = mysql_query("SELECT * FROM tb_emergency_stock WHERE id_emergency_stock=$id_stok");
	$dataSup = mysql_fetch_array($sqlSup);
	$status_proses = ($dataSup['status_proses']==1)? "checked":"";
			if($dataSup['status_proses']==1){
				$readonly = " readonly";
			}else{
				$readonly = " ";
			}

	if(!empty($id_stok)){
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Ubah Stok Emergency</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}else{
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Tambah Stok Emergency</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}
?>
<div class="table">
	<form method="POST" action="?pages=stock_emergency&act=update">
	<input type="hidden" name="id_stok" value="<?php echo $dataSup['id_emergency_stock'];?>">
	<?php
		if ($dataSup['status'] == 0) {
			$stk = 0 ;
		}else{

			$stk = $dataSup['jumlah'];
		}
	?>
	<input type="hidden" name="awal_stok" value="<?php echo $stk;?>">
	<table style="width:auto">
		<tbody>
			<tr>
				<td><label>Tanggal Stok</label></td>
				<td>:</td>
				<td><input type="text" value="<?php echo $dataSup['tgl'];?>" size="60" name="tgl" id="tgl" placeholder="Tanggal"></td>
			</tr>
			<tr>
				<td><label>Jumlah</label></td>
				<td>:</td>
				<td><input type="text" value="<?php echo $dataSup['jumlah'];?>" size="60" name="jumlah" id="jumlah" placeholder="Jumlah Stok" required>
			    <input type="hidden" value="<?php echo $dataSup['jumlah'];?>" size="60" name="jumlah_tmp" id="jumlah_tmp" placeholder="Jumlah Stok"/></td>
			</tr>
			<tr>
				<td><label>Satuan</label></td>
				<td>:</td>
				<td><input type="text" value="<?php echo $dataKode['remainder'];?>" size="30" name="satuan" id="satuan" value="Karton" placeholder="Karton" readonly="true"></td>
			</tr>
			<tr>
			  <td>Status</td>
			  <td>:</td>
			  <td><select name="status" style="width:202px">
                <option value="1" <?php echo $status = ($dataSup['status']==1)? "selected":"";?>>RTU => RTS</option>
                <option value="0" <?php echo $status = ($dataSup['status']==0)? "selected":"";?>>RTS => RTU</option>
              </select></td>
		  </tr>
			<tr>
				<td><label>Status Proses </label></td>
				<td>:</td>
				<td><input type="checkbox" name="status_proses" id="status" <?php echo $readonly;?>  <?php echo $status_proses;?> /></td> 
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>
					<input type="hidden" value="" size="15" name="id">
					<input type="submit" value="Simpan" name="submit" class="btn btn-primary">
					<input type="Reset" value="Reset" class="btn">				</td>
			</tr>
		</tbody>
	</table>
	<?php
	$sqlRtu = mysql_query("SELECT stock_rtu, stock_rts FROM tb_temp_rts LIMIT 1");
	$getRtu = mysql_fetch_array($sqlRtu);
	?>
	<p>&nbsp;</p>
	<p style="font:bold 14px Tahoma"><i>Total Stock Ready to Use : <?php echo $getRtu['stock_rtu'];?> Karton</i></p>
	<p style="font:bold 14px Tahoma"><i>Total Stock Ready to Sale : <?php echo $getRtu['stock_rts'];?> Karton</i></p>
	</form>
</div>
<?php
}elseif ($act=='update') {
	# code...
	$id_stok = (int) $_POST['id_stok'];
	$tanggal = $_POST['tgl'];
	$jumlah = $_POST['jumlah'];
	$stok_awal = $_POST['awal_stok'];
	$satuan = 'Karton';
	$status = $_POST['status'];
	$status_proses = (isset($_POST['status_proses']))? '1':'0';
	$jumlah_tmp = $_POST['jumlah_tmp'];
	

	
	if(!empty($id_stok)){		

		//RTS => RTU
		if ($status_stock == 0) {
			//recount stock
			$sql_temp = mysql_query("SELECT * FROM tb_temp_rts WHERE id_temp = 1 ");
			$j_stok = mysql_fetch_array($sql_temp);			
			$jml_stok_rts = $j_stok['stock_rts'] + $jumlah_tmp;
			$sql_update_stok_rts = mysql_query("UPDATE tb_temp_rts SET stock_rts = $jml_stok_rts WHERE id_temp = 1");
			$jml_stok_rtu = $j_stok['stock_rtu'] - $jumlah_tmp;
			$sql_update_stok_rtu = mysql_query("UPDATE tb_temp_rts SET stock_rtu = $jml_stok_rtu WHERE id_temp = 1");
			//count update value
			$sql_temp = mysql_query("SELECT * FROM tb_temp_rts WHERE id_temp = 1 ");
			$k_stok = mysql_fetch_array($sql_temp);
			$jml_stok_rts = $k_stok['stock_rts'] - $jumlah;
			$sql_update_stok_rts = mysql_query("UPDATE tb_temp_rts SET stock_rts = $jml_stok_rts WHERE id_temp = 1");
			$jml_stok_rtu = $k_stok['stock_rtu'] + $jumlah;
			$sql_update_stok_rtu = mysql_query("UPDATE tb_temp_rts SET stock_rtu = $jml_stok_rtu WHERE id_temp = 1");
		}
		
		//RTU => RTS
		if ($status_stock == 1) {
			//recount stock
			$sql_temp = mysql_query("SELECT * FROM tb_temp_rts WHERE id_temp = 1 ");
			$j_stok = mysql_fetch_array($sql_temp);			
			$jml_stok_rts = $j_stok['stock_rts'] - $jumlah_tmp;
			$sql_update_stok_rts = mysql_query("UPDATE tb_temp_rts SET stock_rts = $jml_stok_rts WHERE id_temp = 1");
			$jml_stok_rtu = $j_stok['stock_rtu'] + $jumlah_tmp;
			$sql_update_stok_rtu = mysql_query("UPDATE tb_temp_rts SET stock_rtu = $jml_stok_rtu WHERE id_temp = 1");
			//count update value
			$sql_temp = mysql_query("SELECT * FROM tb_temp_rts WHERE id_temp = 1 ");
			$k_stok = mysql_fetch_array($sql_temp);
			$jml_stok_rts = $k_stok['stock_rts'] + $jumlah;
			$sql_update_stok_rts = mysql_query("UPDATE tb_temp_rts SET stock_rts = $jml_stok_rts WHERE id_temp = 1");
			$jml_stok_rtu = $k_stok['stock_rtu'] - $jumlah;
			$sql_update_stok_rtu = mysql_query("UPDATE tb_temp_rts SET stock_rtu = $jml_stok_rtu WHERE id_temp = 1");
		}
		
		$update = mysql_query("UPDATE tb_emergency_stock SET 
							tgl='$tanggal',
							jumlah='$jumlah', 
							satuan='$satuan', 
							status='$status',
							status_proses='$status_proses' 
							WHERE id_emergency_stock=$id_stok");

		if($update)
			sukses('Data berhasil disimpan', '?pages=stock_emergency');
	}else{
		
		//RTS => RTU
		if ($status == 0) {
			$sql_temp = mysql_query("SELECT * FROM tb_temp_rts WHERE id_temp = 1 ");
			$j_stok = mysql_fetch_array($sql_temp);
			if($j_stok['stock_rts'] < $jumlah){
				sukses('Maaf jumlah barang yang diambil melebihi stok ', '?pages=stock_emergency&act=edit');
			}else{				
				$jml_stok_rts = $j_stok['stock_rts'] - $jumlah;
				$sql_update_stok_rts = mysql_query("UPDATE tb_temp_rts SET stock_rts = $jml_stok_rts WHERE id_temp = 1");
				$jml_stok_rtu = $j_stok['stock_rtu'] + $jumlah;
				$sql_update_stok_rtu = mysql_query("UPDATE tb_temp_rts SET stock_rtu = $jml_stok_rtu WHERE id_temp = 1");
			}
		}
		
		//RTU => RTS
		if ($status == 1) {
			$sql_temp = mysql_query("SELECT * FROM tb_temp_rts WHERE id_temp = 1 ");
			$j_stok = mysql_fetch_array($sql_temp);
			if($j_stok['stock_rtu'] < $jumlah){
				sukses('Maaf jumlah barang yang diambil melebihi stok ', '?pages=stock_emergency&act=edit');
			}else{
				$jml_stok_rts = $j_stok['stock_rts'] + $jumlah;
				$sql_update_stok_rts = mysql_query("UPDATE tb_temp_rts SET stock_rts = $jml_stok_rts WHERE id_temp = 1");
				$jml_stok_rtu = $j_stok['stock_rtu'] - $jumlah;
				$sql_update_stok_rtu = mysql_query("UPDATE tb_temp_rts SET stock_rtu = $jml_stok_rtu WHERE id_temp = 1");
			}
		}
		
		if($sql_update_stok_rts && $sql_update_stok_rtu){
			$insert = mysql_query("INSERT INTO tb_emergency_stock VALUES(NULL,'$tanggal', '$jumlah', '$satuan', '$status', '$status_proses')");
			sukses('Data berhasil disimpan', '?pages=stock_emergency');
		}
	}
	
}elseif ($act=='del') {
	# code...
	$id_stok = (int) filter($_GET['id']);

	$sql_cek = mysql_query("SELECT * FROM tb_emergency_stock WHERE id_emergency_stock=$id_stok");
	$cek = mysql_fetch_array($sql_cek);
	$jumlah_stock = $cek['jumlah'];
	$status_stock = $cek['status'];
	
	
	//RTS => RTU
	if ($status_stock == 0) {
		$sql_temp = mysql_query("SELECT * FROM tb_temp_rts WHERE id_temp = 1 ");
		$j_stok = mysql_fetch_array($sql_temp);
		$jml_stok_rts = $j_stok['stock_rts'] + $jumlah_stock;
		$sql_update_stok_rts = mysql_query("UPDATE tb_temp_rts SET stock_rts = $jml_stok_rts WHERE id_temp = 1");
		$jml_stok_rtu = $j_stok['stock_rtu'] - $jumlah_stock;
		$sql_update_stok_rtu = mysql_query("UPDATE tb_temp_rts SET stock_rtu = $jml_stok_rtu WHERE id_temp = 1");
	}
	
	//RTU => RTS
	if ($status_stock == 1) {
		$sql_temp = mysql_query("SELECT * FROM tb_temp_rts WHERE id_temp = 1 ");
		$j_stok = mysql_fetch_array($sql_temp);
		$jml_stok_rts = $j_stok['stock_rts'] - $jumlah_stock;
		$sql_update_stok_rts = mysql_query("UPDATE tb_temp_rts SET stock_rts = $jml_stok_rts WHERE id_temp = 1");
		$jml_stok_rtu = $j_stok['stock_rtu'] + $jumlah_stock;
		$sql_update_stok_rtu = mysql_query("UPDATE tb_temp_rts SET stock_rtu = $jml_stok_rtu WHERE id_temp = 1");
	}
	
	$delete = mysql_query("DELETE FROM tb_emergency_stock WHERE id_emergency_stock=$id_stok");

	if($delete)
		sukses('Data berhasil dihapus', '?pages=stock_emergency');
}
?>
